﻿using System;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.WeChatMall.Domain.Entitys.AppInfo;
using ZilLion.Service.WeChatMall.Domain.Repositorys.Interface.AppInfo;

namespace ZilLion.Service.WeChatMall.Domain.Repositorys.AppInfo
{
    public class AppShopRelationShipReponsitory : SqlServerRespository<AppShopRelationShipEntity>,
        IAppShopRelationShipReponsitory
    {
        public AppShopRelationShipReponsitory()
        {
            Context = new WechatMallContext();
        }

        public AppShopRelationShipEntity GetRelationShipByShopid(long shopid)
        {
            var querysql = DapperHelper.CreateColSqlByMap<AppShopRelationShipEntity>();
            var sql = $@"SELECT {querysql} FROM appshoprelationship
            WHERE shopid=@shopid";
            var result = GetList(sql, new { shopid }).FirstOrDefault();
            return result;
        }

        public void RemoveRelationShipByAppId(string appid, UnitOfWork unit = null)
        {
            DapperHelper.Execute($"delete from appshoprelationship where appid=@appid", Context, unit, new { appid });
        }

        public void AddRelationShip(AppShopRelationShipEntity entity, UnitOfWork unit = null)
        {
            entity.UpdatedTime = DateTime.Now;
            Add(entity, unit);
        }

        public void RemoveRelationShip(AppShopRelationShipEntity entity, UnitOfWork unit = null)
        {
            entity.UpdatedTime = DateTime.Now;
            Remove(entity, unit);
        }

        public void RemoveRelationByShopid(long shopid, UnitOfWork unit = null)
        {
            DapperHelper.Execute("DELETE FROM appshoprelationship WHERE shopid=@shopid", Context, unit, new { shopid });
        }
    }
}